<%@page import="java.util.UUID"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@ include file="/WEB-INF/pages/common/taglib.jsp" %>
<%
    String uuid = UUID.randomUUID().toString();
    request.setAttribute("uuid", uuid);
%>
<jsp:include page="/WEB-INF/pages/platform/optAuth/includeOptAuthGrid.jsp"/>
<jsp:include page="/WEB-INF/pages/platform/optBtn/includeOptBtnGrid.jsp"/>

<div class="box box-primary">
	<div class="row">
		<div class="col-md-3" id="dataModelGroup_${uuid}">
			<div class="box-header with-border">
				<h3 class="box-title">
				</h3>
				<div class="box-tools pull-right">
				</div>
			</div>
			<div class="box-body">
			
			</div>
		</div>
		<div class="col-md-9">
			<div class="box-header with-border">
				<h3 class="box-title">
               		<div id="dataModelGroup_${uuid}"></div>
               </h3><!-- /.box-title -->
               	<div class="box-tools pull-right" id="queryDefGridTb_${uuid}">
               		<shiro:hasPermission name="QUERY_DEF_ADD">
		    		<button class="btn  btn-sm btn-success addBtn">
		    			<span class="glyphicon glyphicon-plus" aria-hidden="true"></span> 添加
		    		</button>
		    		</shiro:hasPermission>
		    		
		    		<shiro:hasPermission name="QUERY_DEF_MOD">
		    		<button class="btn btn-sm btn-primary editBtn">
		    			<span class="glyphicon glyphicon-pencil" aria-hidden="true"></span> 修改
		    		</button>
		    		</shiro:hasPermission>
		    		
		    		<shiro:hasPermission name="QUERY_DEF_DEL">
		    		<button class="btn btn-sm btn-danger delBtn">
		    			<span class="glyphicon glyphicon-trash" aria-hidden="true"></span> 删除
		    		</button>
		    		</shiro:hasPermission>
		    		
		    		<shiro:hasPermission name="QUERY_DEF_MOD">
						<div class="btn-group">
							  <button type="button" class="btn btn-sm btn-default dropdown-toggle" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
							   	 交换 <span class="caret"></span>
							  </button>
							  <ul class="dropdown-menu">
							    <li><a href="javascript:void(0)" class="expBtn">导出</a></li>
							    <li><a href="javascript:void(0)" class="impBtn" id="impQueryDefBtn">导入</a></li>
							  </ul>
						</div>
					</shiro:hasPermission>
               	</div>
			</div>
			<div class="box-body">
				<table id="queryDefGrid_${uuid}"></table>
			</div>
		</div>
	</div>
</div>

<div class="row">
	<div class="col-md-12" id="columnslayout_${uuid}">
			<div class="nav-tabs-custom">
				<ul class="nav nav-tabs" role="tablist">
					<li role="presentation" class="active">
						<a href="#queryDefTab_${uuid}" role="tab" data-toggle="tab">
		    			<span class="glyphicon glyphicon-th-list" aria-hidden="true"></span> 显示字段
		    			</a>
					</li>
					<li role="presentation">
						<a href="#searchColDefTab_${uuid}" role="tab" data-toggle="tab">
		    			<span class="glyphicon glyphicon-search" aria-hidden="true"></span> 查询条件
		    			</a>
					</li>
					<li role="presentation">
						<a href="#optBtnAuthTab_${uuid}" role="tab" data-toggle="tab">
		    			<span class="glyphicon glyphicon-check" aria-hidden="true"></span> 操作权限
		    			</a>
					</li>
					<li role="presentation">
						<a href="#scriptDefTab_${uuid}" role="tab" data-toggle="tab">
		    			<span class="glyphicon glyphicon-list-alt" aria-hidden="true"></span> 自定义查询脚本
		    			</a>
					</li>
				</ul>
				<div class="tab-content">
					<div title="查询字段" id="queryDefTab_${uuid}" role="tabpanel" class="tab-pane fade in  active" style="overflow:auto;padding:10px;">
						
					</div>
					<div title="条件字段" id="searchColDefTab_${uuid}" role="tabpanel" class="tab-pane fade" style="overflow:auto;padding:10px;">
						
					</div>
					<div title="操作权限" id="optBtnAuthTab_${uuid}" role="tabpanel" class="tab-pane fade am-margin-top-sm" >
						<div id="optTab_${uuid}">
							<ul class="nav nav-tabs" role="tablist">
								<li role="presentation" class="active">
									<a href="#optBtnTab_${uuid}" role="tab" data-toggle="tab">
					    			<span class="glyphicon glyphicon-play" aria-hidden="true"></span> 按钮配置
					    			</a>
								</li>
								<li role="presentation">
									<a href="#optAuthTab_${uuid}" role="tab" data-toggle="tab">
					    			<span class="glyphicon glyphicon-cog" aria-hidden="true"></span> 操作配置
					    			</a>
								</li>
							</ul>
							<div class="tab-content">
						   		<div title="按钮设置" id="optBtnTab_${uuid}" role="tabpanel" class="tab-pane fade in  active" >
									
									<div >
										<div class="box-header with-border">
							               <h3 class="box-title">
							               </h3><!-- /.box-title -->
							               
							               <div class="box-tools pull-right" id="optBtnGridTB_${uuid}">
							               		<shiro:hasPermission name="OPTBTN_ADD">
									    		<button class="btn btn-sm btn-success batAddBt">
									    			<span class="glyphicon glyphicon-th-list" aria-hidden="true"></span> 批量添加
									    		</button>
									    		</shiro:hasPermission>
									    		
									    		<shiro:hasPermission name="OPTBTN_ADD">
									    		<button class="btn btn-sm btn-success addBt">
									    			<span class="glyphicon glyphicon-plus" aria-hidden="true"></span> 添加
									    		</button>
									    		</shiro:hasPermission>
									    		
									    		<shiro:hasPermission name="OPTBTN_MOD">
									    		<button class="btn btn-sm btn-primary editBt">
									    			<span class="glyphicon glyphicon-pencil" aria-hidden="true"></span> 修改
									    		</button>
									    		</shiro:hasPermission>
									    		
									    		<shiro:hasPermission name="OPTBTN_AUTH">
									    		<button class="btn btn-sm btn-info roleAuthBt">
									    			<span class="glyphicon glyphicon-check" aria-hidden="true"></span> 授权
									    		</button>
									    		</shiro:hasPermission>
									    		
									    		<shiro:hasPermission name="OPTBTN_DEL">
									    		<button class="btn btn-sm btn-danger delBt">
									    			<span class="glyphicon glyphicon-trash" aria-hidden="true"></span> 删除
									    		</button>
									    		</shiro:hasPermission>
							               </div>
							             </div>
							             <div class="box-body">
											<table id="optBtnGrid_${uuid}" style="width: 100%">
	            	 
	            							 </table>
										</div>
									</div>
									
									
									
						       	</div>
						   		<div title="操作权限" id="optAuthTab_${uuid}" role="tabpanel" class="tab-pane fade">
									<div >
										<div class="box-header with-border">
							               <h3 class="box-title">
							               </h3><!-- /.box-title -->
							               
							               <div class="box-tools pull-right" id="optAuthGridTB_${uuid}">
							               		<shiro:hasPermission name="OPTAUTH_ADD">
							    	 			<button class="btn btn-sm btn-success batAddBt">
									    			<span class="glyphicon glyphicon-th-list" aria-hidden="true"></span> 批量添加
									    		</button>
									    		</shiro:hasPermission>
									    		
									    		<shiro:hasPermission name="OPTAUTH_ADD">
									    		<button class="btn btn-sm btn-success addBt">
									    			<span class="glyphicon glyphicon-plus" aria-hidden="true"></span> 添加
									    		</button>
									    		</shiro:hasPermission>
									    		
									    		<shiro:hasPermission name="OPTAUTH_MOD">
									    		<button class="btn btn-sm btn-primary editBt">
									    			<span class="glyphicon glyphicon-pencil" aria-hidden="true"></span> 修改
									    		</button>
									    		</shiro:hasPermission>
									    		
									    		<shiro:hasPermission name="OPTAUTH_AUTH">
									    		<button class="btn btn-sm btn-info roleAuthBt">
									    			<span class="glyphicon glyphicon-check" aria-hidden="true"></span> 授权
									    		</button>
									    		</shiro:hasPermission>
									    		
									    		<shiro:hasPermission name="OPTAUTH_DEL">
									    		<button class="btn btn-sm btn-danger delBt">
									    			<span class="glyphicon glyphicon-trash" aria-hidden="true"></span> 删除
									    		</button>
									    		</shiro:hasPermission>
							               </div>
							             </div>
							             <div class="box-body">
											<table id="optAuthGrid_${uuid}" style="width: 100%"></table>
										</div>
							         </div>
						       	</div>
					       	</div>
					    </div>
					</div>
					<div title="自定义查询脚本" id="scriptDefTab_${uuid}"  role="tabpanel" class="tab-pane fade"  style="overflow:auto;padding:10px;">
						
					</div>
				</div>
			</div>
	</div>
</div>

<script type="text/javascript">
$(function(){
	var uuid = '${uuid}';
	var dataModelGroupTree;
	require(['wiseMen/dataModel/dataModelGroup'],function(DataModelGroup){
		dataModelGroupTree=new DataModelGroup({
			render:$("#dataModelGroup_"+uuid),
			onClick:function(treeNode){
				var dataModelGroupCode = treeNode.code;
                queryDefGrid.dataModelGroupCode = dataModelGroupCode;
                queryDefGrid.bootstrapTable("refresh");
			}
		});
	});
	
	var optAuthGrid = new wiseMen.platform.optAuthGrid({
    	gridDom:$("#optAuthGrid_"+uuid),
    	toolbarSelector:"#optAuthGridTB_"+uuid
    });
    
    var optBtnGrid=new wiseMen.platform.optBtnGrid({
    	gridDom:$("#optBtnGrid_"+uuid),
    	toolbarSelector:"#optBtnGridTB_"+uuid
    });
	
	var queryDefGrid=$("#queryDefGrid_"+uuid).bootstrapTable({
		columns:[{
			field : '_id',
			checkbox : true,
			width:20
		},{
			title : '名称',
			field : 'title',
			width : '120px',
			orderable:false
		},{
			title : '编码',
			field : 'code',
			width : '120px',
			orderable:false
		}],
		url:ctx + "/queryDefAction/loadQueryDefs.do",
		queryParams:function(d){
			if(queryDefGrid){
				if(queryDefGrid.dataModelGroupCode){
					d.dataModelGroupCode=queryDefGrid.dataModelGroupCode;
				}
			}
			return d;
    	},
    	onClickRow:function(row){
    		loadViewColumnDefs(row.id);
        	loadSearchColumnDefs(row.id);
        	loadScriptDefForm(row.id);
        	
        	optAuthGrid.funcPointId = row.funcPointId;
            optAuthGrid.bootstrapTable("refresh");
            
            optBtnGrid.funcPointId = row.funcPointId;
            optBtnGrid.bootstrapTable("refresh");
    	}
	});
	
	var _queryDefGridTb=$("#queryDefGridTb_"+uuid);
	
	_queryDefGridTb.find(".addBtn").click(function(){
		if(queryDefGrid.dataModelGroupCode){
			var sub = top.webUtils.topicSubscribe("queryDefine.edit.success", function (args) {
	            dialog.close();
	            queryDefGrid.bootstrapTable("refresh");
	        });
	        var dialog = editQueryDef({
	        	dataModelGroupCode:queryDefGrid.dataModelGroupCode,
	            onClose: function () {
	                sub.remove();
	            }
	        });
		}else{
			webUtils.error("请选择具体的业务分组");
		}
		
	});
	
	_queryDefGridTb.find(".editBtn").click(function(){
		var rows=queryDefGrid.bootstrapTable("getSelections");
    	if(rows && rows.length>0){
    		var sub = top.webUtils.topicSubscribe("queryDefine.edit.success", function (args) {
        		dialog.close();
        		queryDefGrid.bootstrapTable("refresh");
            });
    		var row=rows[0];
    		var dialog= editQueryDef({
            	id:row.id,
                onClose: function () {
                    sub.remove();
                }
            });
    	} else {
    		webUtils.alert("请选择需要编辑的数据");
        }
        
	});
	
	_queryDefGridTb.find(".delBtn").click(function(){
		
		webUtils.confirm("删除后将不可恢复，请问是否继续?", function(result) {
			if(result){
				var rows=queryDefGrid.bootstrapTable("getSelections");
		    	if(rows && rows.length>0){
		    		var selecteds = [];
		            $.each(rows, function (i, n) {
		                selecteds.push(n.id);
		            });
		            $.post(ctx + "/queryDefAction/delQueryDefByIds.do", {
		                ids: selecteds.join(",")
		            }, function (data) {
		                if (data.success) {
		                	queryDefGrid.bootstrapTable("refresh");
		                } else {
		                	webUtils.alert(data.msg);
		                }
		            }, 'json');
		    	} else {
		    		webUtils.alert("请选择需要删除的数据");
		        }
			}
		});
    	
    });
	
	
	/* 导入导出 */
	_queryDefGridTb.find(".expBtn").click(function(){
    	var rows=queryDefGrid.bootstrapTable("getSelections");
    	if(rows && rows.length>0){
    		var ids = [];
            $.each(rows, function (i, n) {
            	ids.push(n.id);
            });
    		window.open(ctx + "/queryDefAction/export.do?ids="+ids.join(","));
    	} else {
    		webUtils.alert("请选择需要导出的数据");
        }
    });
    
    var queryDefUploader
    webUtils.fileUploader({
    	server:ctx+"/queryDefAction/import.do",
    	uploadPick:"#impQueryDefBtn",
    	duplicate:false,
    	accept: {
            title: 'json文件类型',
            extensions: 'json'
        },
        afterInit:function(u,uder){
        	queryDefUploader=u;
        },
        uploadBeforeSend:function(object , data ,headers){
        	data.dataModelGroupCode=queryDefGrid.dataModelGroupCode;
        }
    });
    
    _queryDefGridTb.find(".impBtn").mouseenter(function(){
    	queryDefUploader.refresh();
    });
	
	var editQueryDef=function(params){
		params=params||{};
		var p=[];
		if(params.id){
			p.push("id="+params.id);
		}
		if(params.dataModelGroupCode){
			p.push("dataModelGroupCode="+params.dataModelGroupCode);
		}
		
		var dialog=webUtils.dialog({
            title: '编辑查询定义',
            url:ctx + '/queryDefAction/queryDefEditView.do?' + p.join("&"),
            onremove: function () {
                if (params.onClose) {
                    params.onClose.call(null);
                }
            }
        });
        return dialog;
	}
	
	function loadViewColumnDefs(queryDefId){
		$("#queryDefTab_"+uuid).html("").load(ctx+"/queryDefAction/viewColumnView.do",{
			queryDefId:queryDefId
		});
	}
	
	function loadSearchColumnDefs(queryDefId){
		$("#searchColDefTab_"+uuid).html("").load(ctx+"/queryDefAction/searchColumn.do",{
			queryDefId:queryDefId
		});
	}
	
	function loadScriptDefForm(queryDefId){
		$("#scriptDefTab_"+uuid).html("").load(ctx+"/queryDefAction/scriptDefFormView.do",{
			queryDefId:queryDefId
		});
	}
	
});

var selectColumn=function(params){
	params=params||{};
	var p=[];
	p.push("id="+queryDefId);
	var dialog =webUtils.dialog({
		title: '请选择字段',
		url:ctx + '/queryDefAction/columnSelectorView.do?' + p.join("&"),
		onremove: function () {
			if (params.onClose) {
                params.onClose.call(null);
            }
	    }
	});
	return dialog;
}
</script>